package org.slsale.controller.user;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.slsale.pojo.User;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class BaseController {

	private Logger logger = Logger.getLogger(BaseController.class);

	private User currentUser;

	public User getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(User currentUser) {
		if (currentUser == null) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
			HttpSession session = request.getSession(false);
			if (session != null) {
				currentUser = (User) session.getAttribute("userSession");
			} else {
				currentUser = null;
			}
		}
		this.currentUser = currentUser;
	}

	/**
	 * 国际时间化
	 * */
	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class,
				new PropertyEditorSupport() {
					final BaseController this$0;

					public void setAsText(String value) {
						try {
							setValue((new SimpleDateFormat("yyyy-MM-dd"))
									.parse(value));
						} catch (ParseException e) {
							setValue(null);
						}
					}

					public String getAsText() {
						return (new SimpleDateFormat("yyyy-MM-dd"))
								.format((Date) getValue());
					}

					{
						this$0 = BaseController.this;
						// super();
					}
				});
	}

}
